/**
 * @file XYXKC
 * @brief XYXKC's ESPONE Mind+ library.
 * @n This is the MIND+expansion module of the Xiaoyingxing patrol car.
 *
 * @copyright    XYXKC,2023
 * @copyright    MIT Lesser General Public License
 *
 * @author [email](hyc@xyxedu.cn)
 * @date  2023-10-25
 */
enum Motor {
    //% block="M1"
    M1,
    //% block="M2"
    M2,
    //% block="M3"
    M3,
    //% block="M4"
    M4,
}

//% color="#00bfff" iconWidth=50 iconHeight=40
namespace XYXKCMotor {


    //% block="electrical machinery[Motor]with[SPEED]%theSpeedOfRotation" blockType="command"
    //% Motor.shadow="dropdown" Motor.options="Motor" Motor.defl="Motor.M1"
    //% SPEED.shadow="range" SPEED.params.min=-100 SPEED.params.max=100 SPEED.defl=0
    export function MotorRun(parameter: any, block: any) {
        let Motor = parameter.Motor.code;
        let speed = parameter.SPEED.code;
        Generator.addInclude(`motor_ib`, `#include<XYXKC_Motor.h>`)
        if(Generator.board === 'firebeetleesp32e'){//如果是arduinouno板，生成如下代码
            Generator.addObject(`motor_ib_Object`, `XYXKC_Motor`,'motor_ib(true)')
        }else if(Generator.board === 'esp32'){//如果是掌控板，生成如下代码
            Generator.addObject(`motor_ib_Object`, `XYXKC_Motor`,'motor_ib(false)')
        }

        Generator.addCode(`motor_ib.motorRun(${'motor_ib.' + Motor},${speed});`)
    }
    //% block="speechRecognitionAndReturningData " blockType="reporter"
    export function vcSpeechRecognitionSoftware(parameter: any, block: any) {
        Generator.addSetup(`vc_initSetup`, `Serial1.begin(9600,33, -1);`);
        Generator.addObject(`defineserialReceiveDataFun`,'uint8_t' ,``+
            `serialReceiveData() {\n`+
            `  uint8_t c;\n`+
            `  static uint8_t inBuf[3];   //数据数组\n`+         // static 必须有，在测试ESPONE 主板时，没有无法正常工作
            `  static uint8_t offset;\n`+
            `  static uint32_t checksum;\n`+
            `  static enum _serial_state { IDLE, HEADER_5A } c_state;\n\n`+
            `  while (Serial1.available()) {\n`+
            `    c = Serial1.read();     // 读串口缓冲区\n`+
            `    if (c_state == IDLE) {        // 串口状态空闲,等待 HEADER 5A 状态的到来\n`+
            `      if (c == 0x5A) {            // 判断是否进入 HEADER 5A\n`+
            `        checksum = c;             // 校验和 1  -  head\n`+
            `        c_state = HEADER_5A;      // 进入 HEADER_5A 状态\n`+
            `        offset = 0;\n`+
            `      } else  c_state = IDLE;     // 返回IDLE状态\n`+
            `    } else if (c_state == HEADER_5A && offset < 3) {\n`+
            `      checksum += c;              // 校验和 2  -  data\n`+
            `      inBuf[offset] = c;\n`+
            `      offset++;\n`+
            `    } else if (c_state == HEADER_5A && offset >= 3) {\n`+
            `      c_state = IDLE;             // 返回IDLE状态\n`+
            `      if ((checksum & 0xFF) == c) {\n`+
            `        return inBuf[0];          // 返回指令值\n`+
            `      }\n`+
            `    }\n`+
            `  }\n`+
            `  return 0x01;                    // 未接收数据，默认返回0x01\n`+
            `}`
        );

        Generator.addCode('serialReceiveData()');
    }

}
